package shopping.web;

import shopping.bean.Clothes;
import shopping.bean.Page;
import shopping.service.ClothesService;
import shopping.service.impl.ClothesServiceImpl;
import shopping.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ClientClothesServlet extends BaseServlet {
    private ClothesService clothesService = new ClothesServiceImpl();

    /**
     * 处理分页功能
     * @param req
     * @param resp
     */
    protected void page(HttpServletRequest req, HttpServletResponse resp){
//        System.out.println("经过了前台的ClothesServlet程序");
        //获取请求的参数---pageNo和pageSize
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //保存pageNo和pageSize数据到Page对象
        Page<Clothes> page = clothesService.page(pageNo,pageSize);
//        System.out.println(123123);
        page.setUrl("client/clothesServlet?action=page");
        //保存Page对象到Request域中
        req.setAttribute("page",page);
        //请求转发到view/manager/clothesmanager.jsp页面
        try {
            req.getRequestDispatcher("/view/client/clothes_client.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    /**
     * 处理分页功能
     * @param req
     * @param resp
     */
    //    通过衣服类别查找
    protected void pageByKind(HttpServletRequest req, HttpServletResponse resp){
//        System.out.println("经过了前台的ClothesServlet程序");
        //获取请求的参数---pageNo和pageSize
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        String kind = req.getParameter("kind");
        //保存pageNo和pageSize数据到Page对象
        Page<Clothes> page = clothesService.pageByKind(pageNo,pageSize,kind);

        //保存Page对象到Request域中
        if (page.getPageTotalCount() == 0) {
            //请求转发到view/manager/clothesmanager.jsp页面
            try {
                req.getRequestDispatcher("/view/error/error.jsp").forward(req,resp);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            StringBuilder builder = new StringBuilder("client/clothesServlet?action=pageByKind");
            //如果有名字，将其追加到builder中
            if(req.getParameter("kind") != null){
                builder.append("&kind=").append(req.getParameter("kind"));
            }
            System.out.println(builder.toString());
            page.setUrl(builder.toString());
            req.setAttribute("page",page);
            //请求转发到view/manager/clothesmanager.jsp页面
            try {
                req.getRequestDispatcher("/view/client/clothes_client.jsp").forward(req,resp);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
//    通过衣服名查找
    protected void pageByName(HttpServletRequest req, HttpServletResponse resp){
//        System.out.println("经过了前台的ClothesServlet程序");
        //获取请求的参数---pageNo和pageSize
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        String name = req.getParameter("name");
        //保存pageNo和pageSize数据到Page对象
        Page<Clothes> page = clothesService.pageByName(pageNo,pageSize,name);

        //保存Page对象到Request域中
        //如果没有查询到
        if (page.getPageTotalCount() == 0) {
            //请求转发到view/manager/clothesmanager.jsp页面
            try {
                req.getRequestDispatcher("/view/error/error.jsp").forward(req,resp);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            StringBuilder builder = new StringBuilder("client/clothesServlet?action=pageByName");
            //如果有名字，将其追加到builder中
            if(req.getParameter("name") != null){
                builder.append("&name=").append(req.getParameter("name"));
            }
            System.out.println(builder.toString());
            page.setUrl(builder.toString());
            req.setAttribute("page",page);
            //请求转发到view/manager/clothesmanager.jsp页面
            try {
                req.getRequestDispatcher("/view/client/clothes_client.jsp").forward(req,resp);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
//同过价格区间查询
    protected void pageByPrice(HttpServletRequest req, HttpServletResponse resp){
//        System.out.println("经过了前台的ClothesServlet程序");
        //获取请求的参数---pageNo和pageSize
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        int min = WebUtils.parseInt(req.getParameter("min"), 0);
        int max = WebUtils.parseInt(req.getParameter("max"), Integer.MAX_VALUE);
        //保存pageNo和pageSize数据到Page对象
        Page<Clothes> page = clothesService.pageByPrice(pageNo,pageSize,min,max);
        StringBuilder builder = new StringBuilder("client/clothesServlet?action=pageByPrice");
        //如果有最小价格，将其追加到builder中
        if(req.getParameter("min") != null){
            builder.append("&min=").append(req.getParameter("min"));
        }
        //如果有最大价格，将其追加到builder中
        if(req.getParameter("max") != null){
            builder.append("&max=").append(req.getParameter("max"));
        }
        System.out.println(builder.toString());
        page.setUrl(builder.toString());
        //保存Page对象到Request域中
        req.setAttribute("page",page);
        //请求转发到view/manager/clothesmanager.jsp页面
        try {
            req.getRequestDispatcher("/view/client/clothes_client.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
